package com.springbootmybatishw.controller;

import com.springbootmybatishw.entity.Student;
import com.springbootmybatishw.service.StudentService;
import com.springbootmybatishw.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController

public class StudentController {
    @Autowired
    StudentService studentService;
    @GetMapping("/stu/{name}")
    public Result<Student> findStudentByName(@PathVariable("name") String name) {
        Student s = studentService.selectStudentByName(name);
        if (s!=null){
            return Result.success("200","查询成功",s);
        }else return  Result.error("202","查询失败");
    }

    @GetMapping("/stus")
    public Result<List<Student>> findStudentAll() {
        return Result.success(studentService.selectStudentAll());
    }

    @GetMapping("/stus/page")
    public Result findStudentAllByPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize){
        return Result.success(studentService.selectAllByPage(pageNum,pageSize));
    }
    @PostMapping("/add")
    public Result insertStudent(Student student) {
        int rows = studentService.addStudent(student);
        if (rows > 0){
            return Result.success("200","增加学生记录成功",rows);
        }else return  Result.error("202","增加学生记录失败");
    }
    @DeleteMapping("/delete/{id}")
    public Result deleteStudent(@PathVariable("id") Integer id) {
        int rows = studentService.deleteStudent(id);
        if (rows > 0){
            return Result.success("200","删除学生记录成功",rows);
        }else return  Result.error("202","删除学生记录失败");
    }
    @PutMapping("/edit")
    public Result updateStudent(Student student) {
        int rows = studentService.editStudent(student);
        if (rows > 0){
            return Result.success("200","修改学生记录成功",rows);
        }else return  Result.error("202","修改学生记录失败");
    }

}
